//七牛token 七牛云平台安全标识
var qiniutoken = '';
var qiniuhost = "http://bqiniu.the-plough.cn/";
//================上传申请的图片=============
function uploadImg(event) {
    var e=window.event||event;
    var that = e.target;
    $(that).attr('value','');
    // 获取当前选中的文件
    var length = e.target.files.length;
    // 判断文件的数量
    if(length > 1){
        $.alert("单次文件上传数量不能大于1张！");
        return;
    }
    // 判断文件的尺寸
    for (var i=0 ;i< length; i++) {
        var file = e.target.files[i];
        if(file.size > 20*1024 * 1024){
            $.alert("文件尺寸不能大于20M");
            return;
        }
    }
    //需要上传的图片内容 base64格式
    var imgStr = '';
    //七牛云平台 配置的域名
    var urlHeader = "http://bqiniu.the-plough.cn/";
    //获取七牛的token
    var qiniuParams = {
        url: '/app/upload/qiniu/token',
        type: 'get',
        data: {},
        header: {},
        async: false,
        callback: function(res){
            qiniutoken = res;
        }
    };
    ajaxRequest(qiniuParams);
    // 追加到from对象
    var allImgSrc = '';
    var is_uploaded = false;
    for (var i=0 ;i< length; i++) {
        imgStr = '';
        var reader = new FileReader();
        var file = e.target.files[i];
        //文件后缀
        var prifix = file.name.split('.')[1];
        //七牛云图片base64格式上传地址
        var uploadUrl = "https://upload.qiniup.com/putb64/-1/key/" + window.btoa("customer_"+ parseInt(Math.random()*9999999) + new Date().getTime()+"_"+parseInt(Math.random()*9999999) + '.' + prifix);
        reader.readAsDataURL(file);
        reader.onload = function(e) {
            //获取到base64格式的图片
            imgStr = reader.result.split(',')[1];

            $('.rotae_dom').show();
            $.showLoading('<br />正在上传');
            $.ajax({
                headers: {
                    'Authorization' : "UpToken " + qiniutoken
                },
                url: uploadUrl,
                type: 'POST',
                data: imgStr,
                success: function (res) {
                    $.hideLoading();
                    $('.rotae_dom').hide();

                    var result = res;
                    var file_path = qiniuhost + result.key;
                    console.log(qiniuhost + result.key);

                    $(that).parent().find('input[type=hidden]').val(file_path);
                    $(that).parent().find('.camerabox').css("background-image", "url("+file_path+")").css('display', 'block');
                }
            });
        }
    }
}

function ajaxRequest(obj) {
    $.ajax({
        url: obj.url,
        type: obj.type,
        data: obj.data,
        headers: obj.header,
        async: obj.async,
        success: function(data) {
            obj.callback(data);
        },
        error: function(e){
            alert(JSON.stringify(e));
        }
    })
}